﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Web.Admin
{
    [Area("Admin")]
    [Authorize]
    public class ExtvideoCateController : Base
    {
        private readonly ExtVideoCateService _extVideoCateService;
        private readonly AdminUserGroupService _adminUserGroupService;
        private readonly LogService _logService;
        public ExtvideoCateController(ExtVideoCateService  extVideoCateService, AdminUserGroupService adminUserGroupService, LogService logService)
        {
            _extVideoCateService = extVideoCateService;
            _adminUserGroupService = adminUserGroupService;
            _logService = logService;
        }


        public async Task<IActionResult> IndexAsync(int json = 0)
        {
            if (json == 1)
            {
                var pagedList = await _extVideoCateService.GetCategoryListAsync();
                return Json(new { code = 0, data = pagedList });
            }
            return View();
        }


        public async Task<IActionResult> AddOrEditAsync(int? id, int edit = 0, int pid = 0)
        {
            if (edit == 1)
            {
                var data = await _extVideoCateService.GetInfo(id);
                return Json(new { status = 1, data });
            }
            var articleCateData = await _extVideoCateService.GetCategoryListAsync();
            ViewBag.articleCateData = articleCateData;
            ViewBag.id = id;
            ViewBag.pid = pid;
            return View();

        }


        [HttpPost]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> CreatePostAsync(EditExtVideoCate cateModel)
        {
            if (ModelState.IsValid)
            {
                ExtVideoCate cate = new()
                {
                    Name = cateModel.Name,
                    Pid = cateModel.Pid,
                    Theme = cateModel.Theme,
                    Type = cateModel.Type,
                    Num = cateModel.Num,
                    Sort = cateModel.Sort
                };
                await _extVideoCateService.InsertAsync(cate);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "新增栏目");
                return Json(new { status = 1, msg = "新增成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }


        [HttpPost, ActionName("EditPost")]
        [XssFieldFilter]//防Xss
        public async Task<IActionResult> EditPostAsync(EditExtVideoCate cateModel, int id, int dyEdit)
        {
            if (dyEdit == 1)
            {
                ExtVideoCate extvideoCate = await _extVideoCateService.GetInfo(id);
                extvideoCate.Sort = cateModel.Sort;
                await _extVideoCateService.UpdateAsync(extvideoCate);
                return Json(new { status = 1, msg = "修改成功！" });
            }

            if (ModelState.IsValid)
            {
                ExtVideoCate articleCate = await _extVideoCateService.GetInfo(id);
                articleCate.Pid = cateModel.Pid;
                articleCate.Name = cateModel.Name;
                articleCate.Theme = cateModel.Theme;
                articleCate.Sort = cateModel.Sort;
                articleCate.Num = cateModel.Num;
                articleCate.Type = cateModel.Type;
                await _extVideoCateService.UpdateAsync(articleCate);
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), " 修改栏目" + articleCate.Name + " id " + id);
                return Json(new { status = 1, msg = "修改成功！" });
            }
            else
            {
                var error = ModelState.GetErrorMessages();
                return Json(new { status = 0, msg = error });
            }
        }

        [HttpPost, ActionName("Delete")]
        public async Task<IActionResult> DeleteAsync(int id)
        {
            if (await _extVideoCateService.DeleteByPriKeyAsync(id) > 0)
            {
                await _logService.InsertLogSqlAsync(await UserInfoAsync(), "删除栏目,Id " + id);
                return Json(new { status = 1, msg = "删除成功！" });
            }
            else
                return Json(new { status = 0, msg = "删除失败！可能因为当前栏目下有内容！" });
        }

    }
}
